package com.fw.ui.view.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

import com.vaadin.server.StreamResource.StreamSource;

public class FileStreamSource implements StreamSource {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private byte[] bytes;

    public FileStreamSource() {
	super();
    }

    public FileStreamSource(byte[] bytes) {
	super();
	this.bytes = bytes;
    }

    public InputStream getStream() {
	try {
	    /* Write the data to a buffer. */
	    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
	    IOUtils.write(this.bytes, byteArrayOutputStream);
	    /* Return a stream from the buffer. */
	    return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
	} catch (IOException e) {
	    return null;
	}
    }

    public byte[] getBytes() {
	return bytes;
    }

    public void setBytes(byte[] bytes) {
	this.bytes = bytes;
    }

}
